package com.cq.smartelebicycle.fragment;

import com.cq.smartelebicycle.MainFragment;
import com.cq.smartelebicycle.R;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class DengGuangFragment extends Fragment implements OnClickListener {

	public static final String TAG_ID = "DengGuangFragment";
	private MainFragment activity;
	private static final int DENG_OFF = 0;
	private static final int DENG_ON = 1;
	private int daDengState = DENG_OFF;
	private int huxiDengState = DENG_OFF;
	private int weiDengState = DENG_OFF;
	private ImageView daDeng;
	private ImageView huxiDeng;
	private ImageView weiDeng;
	private ImageView bbright_jian;
	private ImageView bbright_jia;
	private ImageView bbright_precess;
	private TextView bright_value;
	private int normal;
	//get the Display Metrics
	private int displayWidth;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.dengguang_fragment, container,
				false);
		return view;
	}

	@Override
	public void onResume() {
		super.onResume();
		DisplayMetrics mDisplayMetrics = new DisplayMetrics();
		getActivity().getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);
		displayWidth = mDisplayMetrics.widthPixels;
		Drawable drawable = getActivity().getResources().getDrawable(R.drawable.jindutiao);
		int mWidth = drawable.getIntrinsicWidth();
		int mHeight = drawable.getIntrinsicHeight();
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(displayWidth/2, displayWidth/2*mHeight/mWidth+1);
		bbright_precess.setLayoutParams(layoutParams);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		activity = (MainFragment) getActivity();
		daDeng = (ImageView) activity.findViewById(R.id.da_deng_icon);
		daDeng.setOnClickListener(this);
		huxiDeng = (ImageView) activity.findViewById(R.id.huxi_deng_icon);
		huxiDeng.setOnClickListener(this);
		weiDeng = (ImageView) activity.findViewById(R.id.wei_deng_icon);
		weiDeng.setOnClickListener(this);
		bbright_jian = (ImageView) activity
				.findViewById(R.id.process_jian_backbright);
		bbright_jian.setOnClickListener(this);
		bbright_jia = (ImageView) activity
				.findViewById(R.id.process_jia_backbright);
		bbright_jia.setOnClickListener(this);
		bright_value = (TextView) activity.findViewById(R.id.process_text);
		normal = Settings.System.getInt(activity.getContentResolver(),
				Settings.System.SCREEN_BRIGHTNESS, 255);
		int brightValue = normal * 100 / 255;
		bright_value.setText(brightValue + "%");
		bbright_precess = (ImageView) activity.findViewById(R.id.process_icon);
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		int id = view.getId();
		switch (id) {
		case R.id.da_deng_icon:
			if (daDengState == DENG_OFF) {
				daDengState = DENG_ON;
				daDeng.setImageResource(R.drawable.deng_on);
			} else {
				daDengState = DENG_OFF;
				daDeng.setImageResource(R.drawable.deng_off);
			}
			break;

		case R.id.huxi_deng_icon:
			if (huxiDengState == DENG_OFF) {
				huxiDengState = DENG_ON;
				huxiDeng.setImageResource(R.drawable.deng_on);
			} else {
				huxiDengState = DENG_OFF;
				huxiDeng.setImageResource(R.drawable.deng_off);
			}
			break;

		case R.id.wei_deng_icon:
			if (weiDengState == DENG_OFF) {
				weiDengState = DENG_ON;
				weiDeng.setImageResource(R.drawable.deng_on);
			} else {
				weiDengState = DENG_OFF;
				weiDeng.setImageResource(R.drawable.deng_off);
			}
			break;
			
		case R.id.process_jia_backbright:
			normal += 25;
			if (normal > 255) {
				normal = 255;
			}
			int brightValue = normal * 100 / 255;
			Settings.System.putInt(activity.getContentResolver(),
					Settings.System.SCREEN_BRIGHTNESS, normal);
			bright_value.setText(brightValue + "%");
			break;

		case R.id.process_jian_backbright:
			normal -= 25;
			if (normal < 0) {
				normal = 0;
			}
			int brightValue2 = normal * 100 / 255;
			Settings.System.putInt(activity.getContentResolver(),
					Settings.System.SCREEN_BRIGHTNESS, normal);
			bright_value.setText(brightValue2 + "%");
			break;
		default:
			break;
		}
	}
}
